package com.yueshop.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.yueshop.product.entity.ProductAttrValueEntity;
import com.yueshop.product.service.ProductAttrValueService;
import com.yueshop.product.vo.AttrRespVo;
import com.yueshop.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yueshop.product.service.AttrService;
import com.yueshop.common.utils.PageUtils;
import com.yueshop.common.utils.R;


/**
 * 商品属性
 *
 * @author cr
 * @email cr@gmail.com
 * @date 2021-04-09 09:53:24
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;





    @Autowired
    private ProductAttrValueService productAttrValueService;




    ///product/attr/base/listforspu/{spuId}

    @GetMapping("/base/listforspu/{spuId}")
    public R baseListforspu(@PathVariable("spuId") Long spuId){

      List<ProductAttrValueEntity> list=productAttrValueService.baseAttrListforspu(spuId);

        return R.ok().put("data",list);
    }





    ///${type}/list/${this.catId}
    @RequestMapping("{attrType}/list/{categoryId}")
    //@RequiresPermissions("product:attr:list")
    public R attrList(@RequestParam Map<String, Object> params,
                      @PathVariable("attrType") String type,
                      @PathVariable("categoryId") Long categoryId){
       // PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.queryBaseAttrPage(params,type,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrRespVo respVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save( @RequestBody AttrVo attr){
		attrService.saveAttr(attr);
        return R.ok();
    }

    ///product/attr/update/{spuId}
    /**
     * 修改
     */
    @RequestMapping("/update/{spuId}")
    public R update(@PathVariable("spuId") Long spuId,
                    @RequestBody List<ProductAttrValueEntity> list){
    productAttrValueService.updateSpuAttr(spuId,list);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
